function loadTree()
{
var oServiceLookup = document.getElementById("serviceid");
var aoItems = oServiceLookup.Items();


if (aoItems.length == 1)
{
var serviceId = aoItems[0].id;
loadResourceSpecTree(event.srcElement, "GetTreeExpandResourceGroup", serviceId, true);
}
}

function registerNodeTypes(oTree)
{
oTree.RegisterNodeType("equipment","/_imgs/ico_16_4000.gif", null, null, "ua_label", "description");
oTree.RegisterNodeType("resourcespec","/_imgs/tree/folder.gif", null, null, "ua_label", "ua_label");
oTree.RegisterNodeType("constraintbasedgroup","/_imgs/ico_16_4007.gif", "/_imgs/ico_16_4007.gif", "/_imgs/ico_16_4007.gif", "ua_label", "description");
oTree.RegisterNodeType("team","/_imgs/ico_16_9.gif", "/_imgs/ico_16_9.gif", "/_imgs/ico_16_9.gif", "ua_label", "description");
oTree.RegisterNodeType("systemuser","/_imgs/ico_16_8.gif", null, null, "ua_label", "description");
oTree.RegisterNodeType("ua_node","/_imgs/error/notif_icn_warn16.png", null, null, ".", "");
}

function setUaLabels(oTree)
{

var oXml = oTree.XmlNode;


var aoResourcespecNodes = oXml.selectNodes("//resourcespec");


for (var i = 0; i < aoResourcespecNodes.length ; i++)
{
var oResourceSpecNode = aoResourcespecNodes[i];

var bHasParent = oResourceSpecNode.parentNode.nodeName != "tree";

var bSameSite = DEFAULT_SAMESITE;
var iQuantity = DEFAULT_QUANTITY;
var sObjective = DEFAULT_OBJECTIVE;
var oInsertNode = oXml.ownerDocument.createElement("ua_label");


var oElement = oResourceSpecNode.selectSingleNode("samesite");
if (oElement !== null)
{
bSameSite = oElement.text == "1";
}

oElement = oResourceSpecNode.selectSingleNode("requiredcount");
if (oElement !== null)
{
iQuantity = oElement.text;
}

oElement = oResourceSpecNode.selectSingleNode("objectiveexpression/Expression");
if (oElement !== null)
{
sObjective = oElement.xml;
}

oCData = oXml.ownerDocument.createCDATASection(getRuleDisplayText(bHasParent, iQuantity, sObjective, bSameSite));
if (oCData != null)
{
oInsertNode.appendChild(oCData);
}

oResourceSpecNode.appendChild(oInsertNode);
}


var aoGroupNodes = oXml.selectNodes("//(constraintbasedgroup|team)");


for (var i = 0; i < aoGroupNodes.length ; i++)
{
var oGroupNode = aoGroupNodes[i];
var sName = "";
var oInsertNode = oXml.ownerDocument.createElement("ua_label");


var oElement = oGroupNode.selectSingleNode("name");
if (oElement !== null)
{
sName = oElement.text;
}

oInsertNode.text = sName;

oGroupNode.appendChild(oInsertNode);
}


var aoResourceNodes = oXml.selectNodes("//(systemuser|equipment)");


for (var i = 0; i < aoResourceNodes.length ; i++)
{
var oResourceNode = aoResourceNodes[i];
var sName = "";
var bDisabled = false;
var oInsertNode = oXml.ownerDocument.createElement("ua_label");


var oElement = oResourceNode.selectSingleNode("name");
if (oElement !== null)
{
sName = oElement.text;
}
oElement = oResourceNode.selectSingleNode("isdisabled");
if (oElement !== null)
{
bDisabled = oElement.text == "1";
}

if (bDisabled)
{
oInsertNode.text = formatString(LOCID_SCHEDDLG_DISABLED_RES, sName);
}
else
{
oInsertNode.text = sName;
}

oResourceNode.appendChild(oInsertNode);
}
}

function getStartTemplate()
{
return "";
}

function getRuleDisplayText(bHasParent, iQuantity, sObjective, bSameSite)
{

var sQuantity = (iQuantity == "-1")? LOCID_SCHEDDLG_RSQUANTITYALL : iQuantity;


var sCriteria = "";
var iCriteriaId = convertExpressionToId(sObjective);
if (iCriteriaId != OBJECTIVE_RANDOM_ID)
{
if (iCriteriaId == OBJECTIVE_LEAST_BUSY_ID)
{
sCriteria = LOCID_SCHEDDLG_RSLEASTBUSY;
}
else if (iCriteriaId == OBJECTIVE_MOST_BUSY_ID)
{
sCriteria = LOCID_SCHEDDLG_RSMOSTBUSY;
}
else if (iCriteriaId == OBJECTIVE_CUSTOM_ID)
{
sCriteria = LOCID_SCHEDDLG_RSCUSTOM;
}
}


var sSite = "";
if (!bHasParent)
{
sSite = (bSameSite == "1") ? LOCID_SCHEDDLG_RSSITESAME : LOCID_SCHEDDLG_RSSITEANY;
}


var sFormatName = "";
if (sCriteria.length > 0)
{
if (sSite.length > 0)
{

sFormatName = LOCID_SCHEDDLG_FMTNMCS;
}
else
{

sFormatName = LOCID_SCHEDDLG_FMTNMC_;
}
}
else
{
if (sSite.length > 0)
{

sFormatName = LOCID_SCHEDDLG_FMTNM_S;
}
else
{

sFormatName = LOCID_SCHEDDLG_FMTNM__;
}
}

return formatString(sFormatName, sQuantity, sCriteria, sSite);
}
